const config = require("../../config/config");
const {
  schoolList,
} = require('../../api/api')
// components/selector/selector.js
function filterArr(selected, selectData) {
  let c = selectData.filter((item) => {
    return (selected.indexOf(item.id) > -1)
  })
  return c
}
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    isTop: {
      type: Boolean,
      value: false
    },
    filterKey: {
      type: String,
      required: true
    },
    list: {
      type: Array,
      required: true,
    },
    selectedData: {
      type: Object,
      required: false
    },
    hidden: {
      type: Boolean,
      default: false
    },
    filterBatch: {
      type: Array,
      value: []
    },
    categoryBatch: {
      type: Array,
      value: []
    },
    setting: {
      type: Object,
      value: {
        visible: true
      }
    },
    mode: {
      type: String,
      value: 'absolute' //相对定位
    },
    pageNo: {
      type: Number,
      value: 1
    },
  },
  observers: {
    'selectedData': function (selectedData) {
      this.syncData(selectedData);
      if (!this.data.isInited) {
        this.setData({
          isInited: true
        })
      }
    },
    'list': function (list) {

      this.syncList(list)
    },

  },
  /**
   * 组件的初始数据
   */
  data: {
    isInited: false,
    currentTab: -1,
    visible: false,
    visibleKey: 'major',
    selectedMajorList: [],
    showFilterBox: false,
    schoolList: [],
    name: '',
    isFocus: false,

  },
  /**
   * 组件的方法列表
   */
  methods: {
    clearName() {
      const {
        selectedData
      } = this.data
      selectedData['search'] = ''
      this.setData({
        schoolList: [],
        name: '',
        isFocus: false,
        selectedData
      })
    },
    focus(e) {
      this.setData({
        isFocus: true
      })
    },
    blur(e) {
      const {
        name
      } = this.data
      if (name.length > 0) {
        this.setData({
          isFocus: true
        })
        return
      }
      this.setData({
        isFocus: false
      })
    },
    cancel() {
      const {
        selectedData
      } = this.data
      if (selectedData['search'] == '') {
        this.setData({
          schoolList: [],
          name: '',
          isFocus: false,
          selectedData,
          currentTab: -1
        })
        return
      }
      selectedData['search'] = ''
      this.setData({
        schoolList: [],
        name: '',
        isFocus: false,
        selectedData,
        currentTab: -1
      })
      this.triggerEvent("onSelectChanged", this.data.selectedData);
    },
    inputChange(e) {
      this.setData({
        name: e.detail.value
      })
      this.schoolSearch()
    },
    schoolSearch() {
      this.setData({
        schoolList: []
      })
      if (this.data.name !== '') {
        this.getList()
      }
    },
    async getList() {
      const {
        name
      } = this.data
      let params = {
        pageNo: 1,
        pageSize: 999,
        name
      }
      const {
        result
      } = await schoolList(params)
      this.setData({
        schoolList: result.records
      })

    },
    syncList(list) {
      const newList = list.filter((item) => {
        return item.key == 'major'
      })
      if (newList.length == 0) {
        this.setData({
          showFilterBox: false
        })
      } else {
        const {
          visibleKey
        } = this.data
        if (visibleKey == 'major') {
          this.setData({
            showFilterBox: true
          })
        }
      }
    },
    syncData1(list) {
      const {
        selectedData
      } = this.data
      let newList = list
      const selectedMajorList = selectedData.selectedMajorList || []
      const selectorMajorList = newList.filter((item) => {
        return item.key == 'major'
      })
      if (selectorMajorList.length <= 0) return
      selectorMajorList[0].items.map((item) => {
        const ids = selectedMajorList.map((item) => {
          return item.id
        })
        const exist = ids.indexOf(item.id) > -1
        item.checked = exist;
      })
      newList[1] = selectorMajorList[0]
      this.setData({
        list: newList
      })


    },
    syncData(selectedData) {
      let {
        list,
        filterBatch,
        categoryBatch,
        visibleKey
      } = this.data;
      if (selectedData.major && visibleKey == 'major') {
        this.setData({
          showFilterBox: true
        })
      }
      list.map(cate => {
        const key = cate.key;
        cate.items.map(item => {
          if (cate.type === 'checkbox') {
            const exist = selectedData[key].length > 0 && selectedData[key].indexOf(item.id) >= 0;
            item.checked = exist;
            // }
          } else if (cate.type === 'radiobox') {
            item.checked = selectedData[key] === item.id;
          }
          return item;
        });
        return cate;
      })
      filterBatch.map(cate => {
        const key = cate.key;
        cate.items.map(item => {
          if (cate.type === 'checkbox') {
            const exist = selectedData[key].indexOf(item.id) >= 0;
            item.checked = exist;
          } else if (cate.type === 'radiobox') {
            item.checked = selectedData[key] === item.id;
          }
          return item;
        });
        return cate;
      })
      categoryBatch.map((item) => {
        const flag = selectedData['category'] === item.id
        item.checked = flag;
        item.children.map((child) => {
          if (Array.isArray(selectedData['categoryId'])) {
            child.checked = flag && selectedData['categoryId'].indexOf(child.id) > -1;
          } else {
            child.checked = flag && selectedData['categoryId'] === child.id;
          }
        })
      })

      this.setData({
        list,
        filterBatch,
        categoryBatch
      })
      this.setData({
        selectedMajorList: selectedData['selectedMajorList']
      })
    },
    //筛选条件需点击确定才生效
    clear() {
      var {
        selectedData,
        filterKey
      } = this.data
      selectedData.major = []
      selectedData.selectedMajorList = []
      this.setData({
        selectedData: selectedData,
        selectedMajorList: []
      })
      wx.setStorageSync(filterKey, selectedData)
      this.triggerEvent("onSelectChanged", selectedData);
    },
    clearItem(e) {
      let {
        id
      } = e.currentTarget.dataset
      var {
        selectedData,
        selectedMajorList,
        filterKey
      } = this.data
      let major = selectedData.major
      const newList = selectedMajorList.filter((item) => {
        return item.id !== id
      })
      major.splice(major.indexOf(id), 1)
      selectedData.selectedMajorList = newList
      selectedData.major = major
      wx.setStorageSync(filterKey, selectedData)
      this.setData({
        selectedData,
        selectedMajorList: newList
      })
      this.triggerEvent("onSelectChanged", selectedData);
    },
    closeMask() {
      let {
        selectedMajorList,
        selectedData,
        filterKey
      } = this.data
      let selectedMajorListStorage = wx.getStorageSync(filterKey).selectedMajorList
      if (selectedMajorListStorage !== undefined) {
        if (selectedMajorList.length !== selectedMajorListStorage.length) {
          selectedMajorList = selectedMajorListStorage
        }
      }
      this.setData({
        currentTab: -1,
        selectedMajorList
      });
    },
    tabSelected(evt) {
      const id = parseInt(evt.target.id);
      var key = evt.currentTarget.dataset.key
      var type = evt.currentTarget.dataset.type
      let flag = id == this.data.currentTab

      let showFilterBox = key == 'major'
      const selectedData = wx.getStorageSync(this.data.filterKey);
      this.setData({
        currentTab: !flag ? id : -1,
        selectedData,
        visible: !flag,
        showFilterBox,
        visibleKey: flag ? 'major' : key
      })
      // if(flag&&type=='search'){
      //   this.triggerEvent("onSelectChanged", this.data.selectedData);
      // }
    },

    tabConfirmed(evt) {
      const {
        key,
      } = evt.currentTarget.dataset;
      this.setData({
        currentTab: -1
      })
      if (key !== undefined) {
        this.data.selectedData.key = key
        this.triggerEvent("onSelectChanged", this.data.selectedData);
      }
    },
    reset(evt) {
      let {
        selectedData
      } = this.data
      const {
        key
      } = evt.currentTarget.dataset
      console.log(key)
      if (selectedData[key].length == 0) return
      if (key == 'category') {
        selectedData['categoryId'] = []
        selectedData[key] = 0
      } else {
        selectedData[key] = []
        selectedData.major = []
        selectedData.selectedMajorList = []
      }
      this.setData({
        selectedData,
        selectedMajorList: []
      })
      this.triggerEvent("onSelectChanged", this.data.selectedData);
    },
    resetBatch() {
      let {
        filterBatch,
        selectedData
      } = this.data
      filterBatch.map((item) => {
        if (item.type === 'checkbox') {
          selectedData[item.key] = []
        } else {
          selectedData[item.key] = 0
        }
      })
      this.setData({
        selectedData
      })
      this.triggerEvent("onSelectChanged", this.data.selectedData);
    },

    search() {
      const {
        name
      } = this.data
      this.triggerEvent("search", {
        name: name
      });
    },
    scroll(e) {
      this.triggerEvent("scroll");
    },
    toggleChecked(evt) {
      const {
        checked,
        key,
        id,
        type,
        value
      } = evt.currentTarget.dataset;
      let selectedData = this.data.selectedData
      const {
        selectedMajorList
      } = this.data
      if (key == 'search') {
        this.setData({
          name: id
        })
        this.schoolSearch()
      }
      if (id == 0 && type == 'checkbox') {
        if (key == 'category') {
          selectedData['categoryId'] = []
          selectedData[key] = 0
        } 
          if (Array.isArray(selectedData[key])) {
            selectedData[key] = [id]
          }
      } else {
        if (key == 'category' && id==0) {
          selectedData['categoryId'] = []
          selectedData[key] = 0
        } 
        if (Array.isArray(selectedData[key])) {
            selectedData[key] = selectedData[key].filter(e => {
              return e
          })
        }
        if (checked) {
          if (key == 'major') {
            const newList = selectedMajorList.filter((item) => {
              return item.id !== id
            })
            selectedData['selectedMajorList'] = newList
            this.setData({
              selectedMajorList: newList
            })
          }
          if (Array.isArray(selectedData[key])) {
            selectedData[key].splice(selectedData[key].indexOf(id), 1);
          }
        } else {
          if (key == 'major') {
            selectedData['selectedMajorList'].push(value)
            this.setData({
              selectedMajorList: selectedData['selectedMajorList']
            })
          }
          if (Array.isArray(selectedData[key])) {
            selectedData[key].push(id);
          } else {
            selectedData[key] = id;
          }
        }
      }

      this.setData({
        selectedData,
        currentTab: (type == 'radiobox') ? -1 : this.data.currentTab
      });

      if (type == 'radiobox')
        this.triggerEvent("onSelectChanged", this.data.selectedData);

    },
  },


})